package com.tqjc.common.ScheduledTask;

import com.qy.led.SendBuffer;
import com.tqjc.common.consts.NormalConst;
import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.utils.PlcOperator;
import com.tqjc.execute.work.CraneWorkFactory;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.service.SteelInfoService;
import com.tqjc.mes.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @projectName: BaotouSteel
 * @className: WordStatusScheduled
 * @author: Patienyon
 * @description: 工作状态定时任务
 * @date: 2023-06-29 16:07
 */
@Service
@Slf4j
public class WorkStatusScheduleTask {

    @Resource
    CraneWorkFactory craneWorkFactory;

    @Resource
    SteelInfoService steelInfoService;
    @Resource
    TestService testService;

    @Resource
    TaskStatus taskStatus;

    @Resource
    PlcOperator plcOperator;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * @param :
     * @return void
     * @author Patienyon
     * @description 更新发送给前端的工作状态
     * @date 2023-06-29 16:24
     */
    @Scheduled(cron = "0/1 * *  * * ? ")
    @Async
    public void updateWorkStatus() {
        List<String> craneList = NormalConst.CRANE_LIST;
        for (String s : craneList) {
            boolean nowLogo = craneWorkFactory.getCraneWorkImpl(s).getData().getStartLogo();
            boolean nowsemiLogo = craneWorkFactory.getCraneWorkImpl(s).getData().getSemiAutoRunFlag();
            if (nowLogo||nowsemiLogo) {
                WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(s);
                String oldStatus = workStatus.getNowStatus();
//                workStatus.setNowStatus("运行中");
                //状态是否发生变化   false：无变化   true:有变化
                boolean flag = false;
                try {
                    String pauseFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, s, "天车交互暂停", false);
//                    log.info("天车：{}监控到天车交互暂停的值为：{}",s,pauseFlag);
                    if("true".equalsIgnoreCase(pauseFlag) && "运行中".equals(oldStatus)){
                        workStatus.setNowStatus("暂停中");
                        flag = true;
                    }
                    if("false".equalsIgnoreCase(pauseFlag) && "暂停中".equals(oldStatus)){
                        workStatus.setNowStatus("运行中");
                        flag = true;
                    }
                    if(StringUtils.isEmpty(oldStatus)|| flag){
                        taskStatus.setWorkStatusByCraneType(s, workStatus);

                    }

                } catch (Exception e){
                    log.error("WorkStatusScheduleTask run error,读取天车交互暂停标签异常。。。");
                }

            } else {
                WorkStatus workStatus = taskStatus.getWorkStatusByCraneType(s);
                String oldStatus = workStatus.getNowStatus();
                if(!"未开始".equals(oldStatus)){
                    workStatus.setNowStatus("未开始");
                    workStatus.setNowStep("");
                    workStatus.setNowSteelNum("");
                    //workStatus.setUnnormalStatus("");
                    workStatus.setNowMode("");
                    workStatus.setNowTask("");
                    taskStatus.setWorkStatusByCraneType(s, workStatus);
                }
            }
        }
    }

    //@Scheduled(cron = "0/4 * *  * * ? ")
    @Async
    public void updateMESSteelInfo() {
        List<SteelInfo> list=steelInfoService.selectAll();
        testService.insertAll(list);
        log.info("共转移钢板信息数据："+list.size()+"条");
    }

    @Scheduled(cron = "0/59 * *  * * ? ")
    @Async
    public void updateLEDCraneComInfo() {
        List<String> craneList = NormalConst.CRANE_LIST;
        for (String s : craneList) {
            try {
                SendBuffer.SendInternalText_Net("湖南天桥嘉成智能科技公司", NormalConst.ledIPMap.get(s), 1, 160, 20, 4, 1,
                        9, 1, 1, 2, 1, 0, 3, false, 0);
            }catch (Exception ex){
                log.error(ex.getMessage(),ex);
                ex.printStackTrace();
            }
        }
    }

    @Scheduled(fixedRate=500)
    @Async
    public void updateLEDCraneStatus() {
        List<String> craneList = NormalConst.CRANE_LIST;
        for (String s : craneList) {
            try {
                String modeVal= HttpPLCUtils.plcReadbc(restTemplate,serverHost,s,"模式",false);
                if("1".equals(modeVal)){
                    SendBuffer.SendInternalText_Net("本地遥控", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if ("2".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("本地机旁", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if ("3".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("本地自动", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if ("4".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("远程手动", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if ("5".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("远程自动", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if ("6".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("远程半自动", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else if("7".equals(modeVal)) {
                    SendBuffer.SendInternalText_Net("一键放料", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else {
                    SendBuffer.SendInternalText_Net("无效模式", NormalConst.ledIPMap.get(s), 1, 104, 20, 1,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }
                String craneIsRun= HttpPLCUtils.plcReadbc(restTemplate,serverHost,s,"大车运行",false);
                if("true".equalsIgnoreCase(craneIsRun)){
                    SendBuffer.SendInternalText_Net("大车运行", NormalConst.ledIPMap.get(s), 1, 104, 20, 2,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else {
                    SendBuffer.SendInternalText_Net("大车停止", NormalConst.ledIPMap.get(s), 1, 104, 20, 2,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }
                String upIsRun= HttpPLCUtils.plcReadbc(restTemplate,serverHost,s,"起升运行",false);
                if("true".equalsIgnoreCase(upIsRun)){
                    SendBuffer.SendInternalText_Net("起升运行", NormalConst.ledIPMap.get(s), 1, 104, 20, 3,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }else {
                    SendBuffer.SendInternalText_Net("起升停止", NormalConst.ledIPMap.get(s), 1, 104, 20, 3,
                            1, 9, 1, 1, 1, 1, 1, 2, false, 0);
                }
            }catch (Exception ex){
                 log.error(ex.getMessage(),ex);
                 ex.printStackTrace();
            }
        }
    }

}
